<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * 处理exhibits表的所有数据
 *
 * type=1雅昌，type=2taoart，type=3美术家，type=4其他小型网站
 *
 *
 */
class exhibitsMakerController extends baseController{

    public function __construct()
    {
        parent::__construct();


    }

    public function index(){
        //$count = model('maexhibits')->count();
        //SELECT * FROM table WHERE id >=2500 ORDER BY auto_id asc LIMIT 0,20
        //it's samll and read all


        //测试片段


        /*//change测试
        $devil['name'] = '谭芷若（）《长相守》12cm x 34cm x 5cm 年代：2017';
        $ret = $this->change($devil);
        print_r($ret);die;*/
        /*//deleteunit测试
        $str = '谭芷若（）《长相守》12cm x 34cm x 5cm 年代：2017';
        //$str = '没图片cm的数据------cm新规则表示cm没图片的不';
        $pos = mb_strpos($str,'没');
        echo $pos,'<br />';
        $unit = 'cm';
        $qian = $this->deleteunit($str,$unit);
        $hou = $this->deleteunit($str,$unit,1);
        echo '删除第一个：',$qian,'<br />';
        echo '删除最后一个：',$hou,'<br />';die;*/
        /*for($i = 1;$i<5;$i++){
            $this->choose($i);
        }*/





        //处理雅昌
        $data_yachang = model('maexhibits')->select('status=0');

        for ($i = 0; isset($data_yachang[$i]); $i++) {
            print_r($data_yachang[$i]);
            $ret = $this->changeyachang($data_yachang[$i]);
            $ret['status'] = '1';
            print_r($ret);
            model('maexhibits')->update('ID="' . $data_yachang[$i]['ID'] . '"', $ret);

        }

        return null;




        //删除没图片的数据------新规则表示没图片的不存，不用删了

        $data = model('maexhibits')->select('status = 0');

        for($i = 0;isset($data[$i]);$i++){
            if($data[$i]['name']!='' && $data[$i]['author'] == 0 && $data[$i]['years'] == ''&&$data[$i]['size'] == ''){
                $ret = $this->change($data[$i]);
                model('maexhibits')->update('ID="'.$data[$i]['ID'].'"',$ret);
            }
        }


        //print_r($data);die;
    }

    //选择处理哪个
    public function choose($type){
        if($type ==1 ) {
            $data_yachang = model('maexhibits')->select('type=1 AND status=0');

            for ($i = 0; isset($data_yachang[$i]); $i++) {
                if ($data_yachang[$i]['name'] != '' && $data_yachang[$i]['author'] == 0 && $data_yachang[$i]['years'] == '' && $data_yachang[$i]['size'] == '') {
                    $ret = $this->changeyachang($data_yachang[$i]);
                    $ret['status'] = '0';
                    model('maexhibits')->update('ID="' . $data_yachang[$i]['ID'] . '"', $ret);
                }
            }
        }

        if($type ==2 ) {
            $data_yachang = model('maexhibits')->select('type=2 AND status=0');

            for ($i = 0; isset($data_yachang[$i]); $i++) {
                if ($data_yachang[$i]['name'] != '' && $data_yachang[$i]['author'] == 0 && $data_yachang[$i]['years'] == '' && $data_yachang[$i]['size'] == '') {
                    $ret = $this->changetaoart($data_yachang[$i]);
                    $ret['status'] = '1';
                    model('maexhibits')->update('ID="' . $data_yachang[$i]['ID'] . '"', $ret);
                }
            }
        }

        if($type ==3 ) {
            $data_yachang = model('maexhibits')->select('type=3 AND status=0');

            for ($i = 0; isset($data_yachang[$i]); $i++) {
                if ($data_yachang[$i]['name'] != '' && $data_yachang[$i]['author'] == 0 && $data_yachang[$i]['years'] == '' && $data_yachang[$i]['size'] == '') {
                    $ret = $this->changemeishujia($data_yachang[$i]);
                    $ret['status'] = '1';
                    model('maexhibits')->update('ID="' . $data_yachang[$i]['ID'] . '"', $ret);
                }
            }
        }

        if($type ==4 ) {
            $data_yachang = model('maexhibits')->select('type=4 AND status=0');

            for ($i = 0; isset($data_yachang[$i]); $i++) {
                if ($data_yachang[$i]['name'] != '' && $data_yachang[$i]['author'] == 0 && $data_yachang[$i]['years'] == '' && $data_yachang[$i]['size'] == '') {
                    $ret = $this->changeqita($data_yachang[$i]);
                    $ret['status'] = '1';
                    model('maexhibits')->update('ID="' . $data_yachang[$i]['ID'] . '"', $ret);
                }
            }
        }
        return null;
    }

    //处理雅昌
    /*
     * 处理原则:
     * 1,正规由空格分为5段，作品名，拥有者（不一定是作者），尺寸，年份，作品类型（tags）
     * 2,有些会多一部分，馆藏，还有些古品会多一些作者，比如苏轼
     * 3,少于5段，或者5段中包括馆藏的都是信息不完整的
     * 4,对于信息不完整的，通过《》取作品名，包括《》的那段全为作品名，取尺寸（change），含有年的段落定义为时间没有时查年代表，剩下的查馆藏，最后剩下的组合定义为作者或收藏人
     *《你是谁》哈哈 1243
     */
    public function changeyachang($data){
        //echo '已进入处理程序';
        $ret = array();

        $size = $this->change($data);
        /*echo 'size::';
        print_r($size);*/
        $name = $size['name'];
        $name = preg_replace('/[\s]+/',' ',$name);

        $pos = mb_strpos($name,'《');
        if($pos !== false){
            $name_copy = mb_substr($name,$pos);
            $postemp = mb_strpos($name_copy,'》');
            $name_copy_copy = mb_substr($name_copy,$postemp+mb_strlen('》'));

            $posend = mb_strpos($name_copy_copy,' ');
            $realpos = $postemp+$posend +1;
            if($posend!==false){
                $ret['name'] = mb_substr($name_copy,0,$realpos);
                $name = $this->deleteunit($name,$ret['name']);
            }
        }


//      echo $name;die;
        $namearr = explode(' ',$name);
        /*echo '分散';
        print_r($namearr);*/
        //print_r($namearr);

        for($i = 0;isset($namearr[$i]);$i++){

            $pos = mb_strpos($namearr[$i],'年');
            if($pos !== false){
                $ret['years'] = $namearr[$i];
                unset($namearr[$i]);
            }
            $pos = mb_strpos($namearr[$i],'世纪');
            if($pos !== false){
                $ret['years'] = $namearr[$i];
                unset($namearr[$i]);
            }

            $pos = mb_strpos($namearr[$i],'x');
            if($pos !== false){
                $ret['size'] = $namearr[$i];
                unset($namearr[$i]);
            }
            $pos = mb_strpos($namearr[$i],"'");
            if($pos !== false){
                $ret['size'] = $namearr[$i];
                unset($namearr[$i]);
            }
            $pos = mb_strpos($namearr[$i],'尺寸');
            if($pos !==false && ($ret['size'] == ''|| $ret['size'] == null || !isset($ret['size'])||$ret['size'] ==false)){
                $ret['size'] = $namearr[$i];
                unset($namearr[$i]);
            }
            $pos = mb_strpos($namearr[$i],'馆藏');
            if($pos !== false){
                unset($namearr[$i]);
            }
            if($namearr[$i]==''){
                unset($namearr[$i]);
            }
        }
//        print_r($namearr);
        /*echo '<----------------->';
        print_r($ret);
        echo '<----------------->';*/

        if(!isset($ret['name'])){
            $ret['name'] = $namearr[0];
            unset($namearr[0]);
        }

        //剩下古作者，拥有者（作者），年代，作品类型
        $namearr = array_merge($namearr);
//        print_r($namearr);
        for($i = 0;isset($namearr[$i]);$i++){
//            echo $i;
            $years = $this->getyears($namearr[$i]);
            if($years != false){
                $ret['years'] = $years;
                unset($namearr[$i]);
                break;
            }else{
                continue;
            }
        }
        $namearr = array_merge($namearr);
        /*echo 'namearr';
        print_r($namearr);*/

        //剩余作者，类型
        if(isset($namearr[0])){
            $ret['author'] = $namearr[0];
        }else{
            $ret['author'] = '';
        }

        $ret['type'] = '';
        for($i = 1;isset($namearr[$i]);$i++){
            $ret['type'] = $ret['type'].$namearr[$i];
        }

        /*if(!isset($namearr[1])){
            $ret['author'] = $namearr[0];
        }
        elseif(isset($namearr[2]) && !isset($namearr[3])){
            $ret['author'] = $namearr[1];
            $ret['type'] = $namearr[2];
        }else{

            $ret['author'] = $namearr[0];
            $i=1;
            $ret['type'] = '';
            while (isset($namearr[$i])){
                $ret['type'] = $ret['type'].$namearr[$i];
                $i++;
            }
        }*/
        /*print_r($ret);

        die;*/
        if(mb_strlen($ret['years'])>12){
            $pos = mb_strpos($ret['years'],'年');
            $ret['years'] = mb_substr($ret['years'],$pos -4,5);
        }
        if(isset($size['size'])) {
            $ret['size'] = $size['size'];
        }elseif ($ret['size']==null){
            $ret['size'] = '';
        }
        if($ret['name'] == ''){
            if($ret['author']!=''){
                $ret['name'] = $ret['author'];
                $ret['author'] = str_replace('作品','',$ret['author']);
            }else {
                $ret['name'] = '无题';
            }
        }

        //echo '最后';
//        print_r($ret);die;
        return $ret;

    }
    //处理
    protected function changetaoart($data){

    }
    //处理
    protected function changemeishujia($data){

    }
    //处理
    protected function changeqita($data){

    }

    //删除()（）,并且取出size
    public function change($data){
//        echo 'change::';
//        print_r($data);
        $ret = array();
        $devil = $data['name'];
        //删除(1/5)这种括号
        $pos = mb_strpos($devil,'（');
        if($pos !== false){
            $pos_end = mb_strpos($devil,'）');
            if($pos_end !== false){
                $kill = mb_substr($devil,$pos,$pos_end+mb_strlen('）')-$pos);
                $devil_temp = str_replace($kill,'',$devil);
                $devil_temp_temp = str_replace(' ','',$devil_temp);
                if($devil_temp_temp  == ''){
                    $devil = mb_substr($devil,$pos+mb_strlen('（'),$pos_end);
                }else{
                    $devil = $devil_temp;
                }
            }
        }

        $pos = mb_strpos($devil,'(');
        if($pos !== false){
            $pos_end = mb_strpos($devil,')');
            if($pos_end !== false){
                $kill = mb_substr($devil,$pos,$pos_end+mb_strlen('）')-$pos);
                $devil_temp = str_replace($kill,'',$devil);
                $devil_temp_temp = str_replace(' ','',$devil_temp);
                if($devil_temp_temp  == ''){
                    $devil = mb_substr($devil,$pos+mb_strlen('（'),$pos_end);
                }else{
                    $devil = $devil_temp;
                }
            }
        }


        //抓取size
        $size_arr = array(
            'x',
            'X',
            '*',
            '×',
        );
        //如果数据只有一段，那处理不了，返回原数据
        //多个空格只留一个
        $devil = preg_replace('/\s+/',' ',$devil);
        $test = explode(' ',$devil);

        /*echo 'test:::';
        print_r($test);
        echo '<br />';*/
        $byebye = 0;
        //print_r($test);die;
        for($i = 0;isset($test[$i]);$i++){
            for($j = 0;isset($size_arr[$j]);$j++){
                $pos = mb_strpos($test[$i],$size_arr[$j]);
                if($pos !== false){
                    //echo '$test[i]::',$test[$i],'<br />long:::',mb_strlen($test[$i]),'<br />';
                    //echo '$size_arr[j]::',$test[$i],'<br />long:::',mb_strlen($test[$i]),'<br />';
                    if(mb_strlen($test[$i]) == mb_strlen($size_arr[$j])){
//                        print_r($size_arr);
                        $pos_temp = mb_strpos($test[$i+2],$size_arr[$j]);
                        if($pos_temp !==false){
//                            echo '<------>';
                            $unit = getlongunit($test[$i-1]);
                            $ret['size'] = $this->getnum($test[$i-1],1).$unit.$test[$i].$test[$i+1].$test[$i+2].$this->getnum($test[$i+3]).$unit;
                            //$unit = getlongunit($test[$i-1]);
                            $num = $this->getnum($test[$i-1],1);
                            $test[$i-1] = $this->deleteunit($test[$i-1],$num.$unit,1);
                            $test[$i-1] = $this->deleteunit($test[$i-1],$num,1);

                            //print_r($test[$i-1]);
                            if($test[$i-1] == ''){
                                unset($test[$i-1]);
                            }
                            unset($test[$i]);
                            unset($test[$i+1]);
                            unset($test[$i+2]);


                            $num = $this->getnum($test[$i+3]);
                            $test[$i+3] = $this->deleteunit($test[$i+3],$num.$unit);
                            $test[$i+3] = $this->deleteunit($test[$i+3],$num);
                            if($test[$i+3] == ''){
                               // unset($test[$i+3]);
                            }
                            $test = array_merge($test);
                            $byebye = 1;
                            break;
                        }else {
//                            echo '>------<';

                            $unit = getlongunit($test[$i-1]);
                            $ret['size'] = $this->getnum($test[$i - 1],1).$unit . $test[$i] . $this->getnum($test[$i + 1]).$unit;

                            $num = $this->getnum($test[$i-1],1);
                            $test[$i-1] = $this->deleteunit($test[$i-1],$num.$unit,1);
                            $test[$i-1] = $this->deleteunit($test[$i-1],$num,1);

                            unset($test[$i]);

                            $num = $this->getnum($test[$i+1]);
                            $test[$i+1] = $this->deleteunit($test[$i+1],$num.$unit);
                            $test[$i+1] = $this->deleteunit($test[$i+1],$num);

                            $test = array_merge($test);
                            $byebye = 1;
                            break;
                        }
                    }else{
                        $ret['size'] = $test[$i];
                        unset($test[$i]);
                        $test = array_merge($test);
                    }


                }
            }
            if($byebye == 1){
                break;
            }
        }

        if($ret['size']==''||!isset($ret['size'])) {
            for ($i = 0; isset($test[$i]); $i++) {
                $danwei = '';
                $danwei = getlongunit($test[$i]);
                if ($danwei != false) {
                    $ret['size'] = $test[$i];
                    unset($test[$i]);
                    break;
                }
            }
            $test = array_merge($test);
        }

        /*echo 'paochusize::';
        print_r($test);
        echo '<br />';*/
        //抓取年代谭芷若《长相守》66cm x 32cm x 3cm 年代：2017
        $ret['name'] = '';
        for($i = 0;isset($test[$i]);$i++){
            $ret['name'] = $ret['name'] .' '. $test[$i];
        }


        //print_r($ret);die;
//        echo 'ready to return ::';
        $ret['name'] = preg_replace('/\s+/',' ',$ret['name']);
        //print_r($ret);
        return $ret;
    }
/*
 * 判断返回年代
 */
    protected function getyears($str){
        if(mb_strlen($str)>1){return false;}
        $years = array(
            '商',
            '周',
            '秦',
            '汉',
            '魏',
            '蜀',
            '吴',
            '晋',
            '隋',
            '唐',
            '辽',
            '宋',
            '金',
            '元',
            '明',
            '清',
            );
        for($i = 0;isset($years[$i]);$i++){
            $pos = mb_strpos($str,$years[$i]);
            if($pos !== false){return $years[$i];}
        }
        return false;
    }

    /*
     * 给一段字符串，返回第一组数字或者最后一组数字,默认第一组
     */
    protected function getnum($str,$endone = null){

        preg_match_all('/\d+/',$str,$arr);

        if($endone != null){
            for($i = 0;isset($arr[0][$i]);$i++){
            }
            return $arr[0][$i-1];
        }else{
            return $arr[0][0];
        }
    }

    /*
     * 字符串删除第一个或最后一个指定字符串，目前用于删除不用的单位
     *$theend = null ,删除第一个匹配，否则删除最后一个匹配
     * $p要删除的字符串
     */
    protected function deleteunit($str,$unit,$theend = null){
        if($theend != null){
            $relpos =0;
            //$lastpos = 0;
            $pos = mb_strpos($str,$unit);
            $temp = $str;
            //echo $str,'<br />';
            //echo 'pos:::',$pos,'<br />';
            if($pos  === false){return $str;}
            while($pos !== false){
//                echo 'while::relpos::',$relpos,'<br />';
//                echo 'while::pos::',$pos,'<br />';

                $relpos = $relpos+$pos+mb_strlen($unit);
                //$lastpos = $pos;
                $temp = mb_substr($temp,$pos+mb_strlen($unit));

//                echo 'temp:::',$temp,'<br />';

                $pos = mb_strpos($temp,$unit);

            }
//            echo 'out::relpos::',$relpos,'<br />';
//            echo 'out::lastpos::',$lastpos,'<br />';
            $relpos -= mb_strlen($unit);
            /*if($relpos != $lastpos){
                $relpos = $relpos + $lastpos;
            }*/

            //echo 'out::relpos::',$relpos,'<br />';
            //echo 'length::unit::',$unit,'<br />';
            $qian = mb_substr($str,0,$relpos);
            //echo 'qian::',$qian,'<br />';
            $hou = mb_substr($str,$relpos+mb_strlen($unit));
            //echo 'hou::',$hou,'<br />';
            $ret = $qian.$hou;

            return $ret;

        }else{
            $pos = mb_strpos($str,$unit);
            if($pos  === false){return $str;}
            $qian = mb_substr($str,0,$pos);
            $hou = mb_substr($str,$pos+mb_strlen($unit));
            $ret = $qian.$hou;
            return $ret;
        }
    }

    /*private function donotcallthis(){

        //作品type
        //纸;纸本;彩打;水墨;墨;布;油画;亚麻;丝绸;绣绷;装订布;速写;油彩;木;根雕;树脂;木料;木刻;版画;水泥;
        //红砖;钢筋;亚克力;玻璃钢;中密度纤维板;石材;铸铜;不锈钢;综合;镜面;钢;雕塑;
        //LED;综合;手工;陶瓷;石;混合;工笔;重彩;水粉;水彩;绢本;装置;影像;行为;照片;视频;装置;摄影;网络素材;观念材料;

        $type = array(
            '纸',
            '纸本',
            '彩打',
            '水墨',
            '水粉水彩',
            '墨',
            '布',
            '油画',
            '亚麻',
            '丝绸',
            '绣绷',
            '装订布',
            '速写',
            '油彩',
            '木',
            '根雕',
            '树脂',
            '木料',
            '木刻',
            '版画',
            '水泥',
            '红砖',
            '钢筋',
            '亚克力',
            '玻璃钢',
            '中密度纤维板',
            '石材',
            '铸铜',
            '不锈钢',
            '综合',
            '镜面',
            '钢',
            '雕塑',
            'LED',
            '综合',
            '手工',
            '陶瓷',
            '石',
            '混合',
            '工笔',
            '重彩',
            '水粉',
            '水彩',
            '绢本',
            '装置',
            '影像',
            '行为',
            '照片',
            '视频',
            '装置',
            '摄影',
            '网络素材',
            '观念材料',
            '其他',
        );

        return ;


    }*/

}
